package com.gjm.demo;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {

    //一个参数
    @RequestMapping("/m1")
    public String method1(String keyword) {
        return "接收参数: " + keyword;
    }

    //两个参数
    @RequestMapping("/m2")
    public String method2(String userName, String keyword) {
        return "userName: " + userName + ", keyword: " + keyword;
    }

    //只能使用GET
    @RequestMapping(value = "/m3", method = RequestMethod.GET)
    public String method3(String keyword) {
        return "接收参数: " + keyword;
    }

    //只能使用GET
    @GetMapping("/m4")
    public String method4(String keyword) {
        return "接收参数: " + keyword;
    }

    //只能使用POST
    @RequestMapping(value = "/m5", method = RequestMethod.POST)
    public String method5(String keyword) {
        return "接收参数: " + keyword;
    }

    //只能使用POST
    @PostMapping("/m6")
    public String method6(String keyword) {
        return "接收参数: " + keyword;
    }

    @RequestMapping("/m7")
    public String method7(Integer num) {
        return "接收参数: " + num;
    }

    @RequestMapping("/m8")
    public String method8(int num) {
        return "接收参数: " + num;
    }

    @RequestMapping("/m9")
    public String method9(UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("/m10")
    public String method10(@RequestParam(value = "q", required = false) String keyword) {
        return "接收参数: " + keyword;
    }

    @RequestMapping("/m11")
    public String method11(int[] arr) {//要么一个一个传，要么一次性传完，不能混合传
        return Arrays.toString(arr);
    }

    @RequestMapping("/m12")
    public String method12(List<Integer> list) {
        return list.toString();
    }

    //传递json
    @RequestMapping("/m13")
    public String method13(@RequestBody UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("/article/{articleID}")
    public String method14(@PathVariable Integer articleID) {
        return "文章ID: " + articleID;
    }

    @RequestMapping("/article/{type}/{articleID}")
    public String method15(@PathVariable @RequestParam(value = "id", required = false) Integer articleID,
                           @PathVariable @RequestParam(value = "ty", required = false) String type) {
        return "文章ID: " + articleID + ", 文章类型: " + type;
    }

    //上传文件
    @RequestMapping("/m16")
    public String method16(MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        return "文件上传成功";
    }
}
